package com.itany.shop.service.proxy;

import com.itany.mybatis.factory.ObjectFactory;
import com.itany.mybatis.tm.TransactionManager;
import com.itany.shop.entity.User;
import com.itany.shop.exception.ServiceException;
import com.itany.shop.exception.ShopException;
import com.itany.shop.service.UserService;
import org.apache.log4j.Logger;

/**
 * Description: 类的描述
 * Author: liudaolin@itany.com
 * Date: 2021/07/21 15:47
 * Version: 1.0 版本信息
 */
public class UserServiceProxy implements UserService {
    Logger logger = Logger.getLogger(UserServiceProxy.class);

    @Override
    public void register(User user, String rePassword) throws ShopException, ServiceException {
        // 执行事务操作
        TransactionManager tm = ObjectFactory.getObj("tm");
        UserService userService = ObjectFactory.getObj("userServiceTarget");
        try {
            tm.begin();
            userService.register(user, rePassword);
            tm.commit();
        } catch (ShopException e) {
            tm.rollback();
            throw e;
        } catch (Exception e) {
            logger.error("UserServiceProxy.register: ", e);
            tm.rollback();
            throw new ServiceException("服务器繁忙");
        }
    }

    @Override
    public void login(User user) throws ShopException, ServiceException {
        TransactionManager tm = ObjectFactory.getObj("tm");
        UserService userService = ObjectFactory.getObj("userServiceTarget");
        try {
            tm.begin();
            userService.login(user);
            tm.commit();
        } catch (ShopException e) {
            tm.rollback();
            throw e;
        } catch (Exception e) {
            tm.rollback();
            logger.error(e);
        }
    }
}
